package project.servlets;

import java.io.IOException;
//import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import schronisko.MySQLConnection;

/**
 * Servlet implementation class AdoptAnimal
 */
@WebServlet("/AdoptAnimal")
public class AdoptAnimal extends HttpServlet {
	private static final long serialVersionUID = 1L;
	HttpSession s;
	static int ID;
	static String status = "waiting";
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		s = request.getSession();
	//	PrintWriter out = response.getWriter();
		ID = (Integer) s.getAttribute("ID");
		
		int ID_animal=Integer.parseInt(request.getParameter("id"));
		System.out.println(ID_animal);
		
		try{
			MySQLConnection.connect().executeUpdate("INSERT INTO adoption(ID_client, ID_animal, status) VALUES ("+ID+", '"+ID_animal+"', '" + status+ "')");
			MySQLConnection.connect().executeUpdate("UPDATE animal SET accessibility = 'pending'  WHERE ID_animal ='"+ID_animal+"'");
    		MySQLConnection.Close();
    		/*out.println("Status adopcji: Oczekujaca. <br />");
    		out.println("Przyjdz do siedziby Schroniska, aby potwierdzic adopcje i odebrac zwierzaka.");
    		out.println("Za chwile zostaniesz przekierownany na stron� glowna schroniska.");
    		response.setHeader("Refresh", "3; URL=index.jsp");*/
    		response.setHeader("Refresh", "0; URL=counters/licznik.jsp");
	}
		catch (SQLException e){
			e.printStackTrace();
		} 

}
}
